Collectors (Java SE 11 & JDK 11 ) 您所在的位置:网站首页 TomAP A multi Collectors (Java SE 11 & JDK 11 )

Collectors (Java SE 11 & JDK 11 )

2024-05-10 12:44| 来源: 网络整理| 查看: 265

Module java.base Package java.util.stream Class Collectors java.lang.Object java.util.stream.Collectors public final class Collectors extends Object Implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.

The following are examples of using the predefined collectors to perform common mutable reduction tasks:

// Accumulate names into a List List list = people.stream() .map(Person::getName) .collect(Collectors.toList()); // Accumulate names into a TreeSet Set set = people.stream() .map(Person::getName) .collect(Collectors.toCollection(TreeSet::new)); // Convert elements to strings and concatenate them, separated by commas String joined = things.stream() .map(Object::toString) .collect(Collectors.joining(", ")); // Compute sum of salaries of employee int total = employees.stream() .collect(Collectors.summingInt(Employee::getSalary)); // Group employees by department Map byDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment)); // Compute sum of salaries by department Map totalByDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.summingInt(Employee::getSalary))); // Partition students into passing and failing Map passingFailing = students.stream() .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD)); Since: 1.8 Method Summary All Methods Static Methods Concrete Methods  Modifier and Type Method Description static  Collector averagingDouble​(ToDoubleFunction


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有